home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
151-175
/
disk_174
/
strings
/
strings.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-06
|
3KB
|
164 lines
/*
* strings : display all ascii strings in a file
* by Joel Swank August 1988
* Version 1.0
*/
#include <stdio.h>
#include <fcntl.h>
#include <exec/types.h>
#define MAX 80 /* size of string buffer */
#define STRINGSIZE 3
#define MAXSTRINGSIZE MAX-1
#define MINSTRINGSIZE 1
#define STDIN 0 /* file descriptor for stdin */
char buffer[MAX];
int stringsize = STRINGSIZE;
int caps = FALSE; /* caps only flag */
int small = FALSE; /* small only flag */
int numerics = FALSE; /* include numerics flag */
FILE *fdopen();
int fileflag = FALSE; /* detect no files - use stdin */
main(argc,argv)
int argc;
char *argv[];
{
register FILE *filep ;
register cnt ;
/* get command line options */
getcmd(argc, argv);
/* get command line filenames */
for (cnt=1; cnt < argc; cnt++)
{ if (*argv[cnt] != '-')
{
if ((filep = fopen(argv[cnt], "r")) == NULL)
fatal("can't open %s", argv[cnt]) ;
printf("FILE: %s\n",argv[cnt]);
dofile(filep);
fclose(filep);
fileflag = TRUE;
}
}
if (!fileflag) /* if no files given, use stdin */
{
filep = fdopen(STDIN, "r");
dofile(filep);
fclose(filep);
}
exit(0);
}
/*
* process open file via stream pointer
*/
dofile(filep)
FILE *filep;
{
register int accept;
register int pos = 0;
register int chr;
while ((chr = getc(filep)) != EOF)
{
accept = FALSE; /* assume invalid */
/* check all requested characters */
if ((chr <= 'z' && chr >= 'a') && !caps ) accept = TRUE;
if ((chr <= 'Z' && chr >= 'A') && !small ) accept = TRUE;
if ((chr <= '9' && chr >= '0') && numerics ) accept = TRUE;
if (chr == '_') accept = TRUE;
if (pos >= MAX-1) accept = FALSE;
if (accept)
{
buffer[pos++] = chr;
} else if (pos != 0) {
buffer[pos] = '\0';
if (strlen(buffer) >= stringsize) printf("%s\n",buffer);
pos = 0;
}
}
}
/*
* fatal - print standard error msg and halt process
*/
fatal(ptrn, data1, data2)
register char *ptrn, *data1, *data2 ;
{
printf("ERROR: ");
printf(ptrn, data1, data2) ;
putchar('\n');
exit(1);
}
/*
* getcmd - get arguments from command line
*/
getcmd(argc, argv)
register argc ;
register char *argv[] ;
{
register cnt ;
/* get command options */
for (cnt=1; cnt < argc; cnt++)
{ if (*argv[cnt] == '-')
{ switch(argv[cnt][1])
{
case 'l':
stringsize = atoi(&argv[cnt][2]) ;
if (stringsize < MINSTRINGSIZE
|| stringsize > MAXSTRINGSIZE)
fatal("Bad -l value: %s", argv[cnt]) ;
break ;
case 'c':
caps = TRUE;
break ;
case 's':
small = TRUE;
break ;
case 'n':
numerics = TRUE;
break ;
case '?': /* help option */
usage();
printf(" Strings Ver 1.0 - display ASCII strings from a file.\n");
printf(" Options:\n");
printf(" lnnn - set minimum length of string to display - default 3.\n");
printf(" c - include capital letters only.\n");
printf(" s - include lower case letters only.\n");
printf(" n - include the digits 0 through 9.\n");
printf(" ? - display this list.\n");
exit(0);
default:
usage();
exit(0);
}
}
}
}
usage()
{
printf("usage:strings [-lnnn] [-c] [-s] [-n] [-?] [file ...]\n");
}